package com.k9sv.service.impl;

import java.util.List;

import org.apache.log4j.Logger;

import com.k9sv.domain.pojo.Album;
import com.k9sv.domain.pojo.Dog;
import com.k9sv.domain.pojo.DogNews;
import com.k9sv.domain.pojo.Photo;

import com.k9sv.service.IDogManager;

public class DogManager extends BaseManager implements IDogManager {

	Logger LOG = Logger.getLogger(DogManager.class);
	
	@Override
	public List<Dog> getDogs(int start, int size){
		try {
			return baseDao.query("from Dog order by created desc", null, start, size);
		} catch (Exception e) {
			LOG.error(e.getMessage());
		}
		return null;
	}

	@Override
	public List<Dog> getCatDog(int catid, int start, int size){
		try {
			return baseDao.query("from Dog where category_id=? order by created desc", new Object[]{catid}, start, size);
		} catch (Exception e) {
			LOG.error(e.getMessage());
		}
		return null;
	}

	@Override
	public List<Dog> getUserDog(int uid, int start, int size) {
		try {
			return baseDao.query("from Dog where ownerId=? order by created desc", new Object[]{uid}, start, size);
		} catch (Exception e) {
			LOG.error(e.getMessage());
		}
		return null;
	}

	@Override
	public List<DogNews> getDogNews(String dogId, int start, int size) {
		try {
			return baseDao.query("from DogNews where dogId=? order by id desc", new Object[]{dogId}, start, size);
		} catch (Exception e) {
			LOG.error(e.getMessage());
		}
		return null;
	}

	@Override
	public int getDogNewsCount(String dogId) {
		try {
			return (Integer)baseDao.getCount("select count(id) from DogNews where dogId=?", new Object[]{dogId}) ;
		} catch (Exception e) {
			LOG.error(e.getMessage());
		}
		return 0;
	}

	@Override
	public List<Album> getAlbums(String dogId, int start, int size) {
		try {
			return baseDao.query("from Album where dogId=? order by id desc", new Object[]{dogId}, start, size);
		} catch (Exception e) {
			LOG.error(e.getMessage());
		}
		return null;
	}

	@Override
	public int getAlbumCount(String dogId) {
		try {
			return (Integer)baseDao.getCount("select count(id) from Album where dogId=?", new Object[]{dogId}) ;
		} catch (Exception e) {
			LOG.error(e.getMessage());
		}
		return 0;
	}

	@Override
	public List<Album> getAlbums(int uid, int start, int size) {
		try {
			return baseDao.query("from DogNews where uid=? order by id desc", new Object[]{uid}, start, size);
		} catch (Exception e) {
			LOG.error(e.getMessage());
		}
		return null;
	}

	@Override
	public int getAlbumCount(int uid) {
		try {
			return (Integer)baseDao.getCount("select count(id) from DogNews where uid=?", new Object[]{uid}) ;
		} catch (Exception e) {
			LOG.error(e.getMessage());
		}
		return 0;
	}

	@Override
	public List<Photo> getPhotos(int albumId, int start, int size) {
		try {
			return baseDao.query("from Photo where albumId=? order by id desc", new Object[]{albumId}, start, size);
		} catch (Exception e) {
			LOG.error(e.getMessage());
		}
		return null;
	}

	@Override
	public int getPhotoCount(int albumId) {
		try {
			return (Integer)baseDao.getCount("select count(id) from Photo where albumId=?", new Object[]{albumId}) ;
		} catch (Exception e) {
			LOG.error(e.getMessage());
		}
		return 0;
	}

	@Override
	public List<Photo> getDogPhoto(String dogId, int start, int size) {
		try {
			return baseDao.query("from Photo where dogId=? order by id desc", new Object[]{dogId}, start, size);
		} catch (Exception e) {
			LOG.error(e.getMessage());
		}
		return null;
	}

	@Override
	public int getDogPhotoCount(String dogId) {
		try {
			return (Integer)baseDao.getCount("select count(id) from Photo where dogId=?", new Object[]{dogId}) ;
		} catch (Exception e) {
			LOG.error(e.getMessage());
		}
		return 0;
	}

	@Override
	public List<Photo> getUserPhoto(int uid, int start, int size) {
		try {
			return baseDao.query("from Photo where uid=? order by id desc", new Object[]{uid}, start, size);
		} catch (Exception e) {
			LOG.error(e.getMessage());
		}
		return null;
	}

	@Override
	public int getUserPhotoCount(int uid) {
		try {
			return (Integer)baseDao.getCount("select count(id) from Photo where uid=?", new Object[]{uid}) ;
		} catch (Exception e) {
			LOG.error(e.getMessage());
		}
		return 0;
	}

}
